<template>
  <el-form :model="formData" ref="queryForm" size="small" :inline="true">
    <template v-for="(item, index) in list">
      <template v-if="item.type == 'inputArr'">
        <el-form-item :prop="item.active" :key="index">
          <div class="select-input-box">
            <el-select v-model="item.active" @change="changeGroup(item)">
              <el-option
                v-for="(option, optInx) in item.label"
                :key="optInx"
                :label="option"
                :value="item.field[optInx]"
              />
            </el-select>
            <el-input
              v-model="formData[item.active]"
              :placeholder="
                '请输入' +
                item.label[item.field.findIndex((el) => el == item.active)]
              "
            ></el-input>
          </div>
        </el-form-item>
      </template>
      <template v-else>
        <el-form-item :label="item.label" :prop="item.field" :key="index">
          <template v-if="item.type == 'input'">
            <el-input
              v-model="formData[item.field]"
              :placeholder="'请输入' + item.label"
            ></el-input>
          </template>
          <template v-if="item.type == 'select'">
            <el-select
              v-model="formData[item.field]"
              :placeholder="'请选择' + item.label"
              clearable
              :multiple="item.multiple"
            >
              <template v-if="item.dataField">
                <slot :name="item.dataField"></slot>
              </template>
              <template v-else>
                <el-option
                  v-for="dict in item.list"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </template>
            </el-select>
          </template>
        </el-form-item>
      </template>
    </template>
    <el-form-item>
      <el-button
        type="primary"
        icon="el-icon-search"
        size="mini"
        @click="handleQuery"
        >搜索</el-button
      >
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  dicts: [],
  props: {
    list: {
      type: Array,
      default: [],
    },
    formData: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {};
  },
  created() {},
  methods: {
    changeGroup(row) {
      row.field.forEach((el) => {
        this.formData[el] = "";
      });
    },
    handleQuery() {
      this.$emit("handleQuery");
    },
    resetQuery() {
      let hasArr = this.list.find((item) => item.type == "inputArr");
      if (hasArr) {
        hasArr.active = hasArr.field[0];
      }
      this.$refs.queryForm.resetFields();
      this.$emit("handleQuery");
    },
  },
};
</script>

<style scoped lang="scss">
.select-input-box {
  display: flex;
  align-items: center;
  ::v-deep .el-select {
    .el-input {
      width: 112px;
      .el-input__inner {
        border-right: 0;
        border-top-left-radius: 4px;
        border-bottom-left-radius: 4px;
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
      }
    }
  }
  ::v-deep .el-input {
    width: 170px;
    .el-input__inner {
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
    }
  }
}
</style>
